package com.fingard.dsp.bank.reqrespobj.authforrec;

import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.RequestBase;
import com.fingard.dsp.bank.reqrespobj.authforrec.detail.ReqAuthForRecDetail;
import com.fingard.dsp.bank.reqrespobj.contract.ReqContract;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

public class ReqAuthForRec extends RequestBase {

    public String totalNum = "";
    public String reqSeqID = "";
    public String payType = "";
    public String ownActName = "";
    public String ownAct = "";
    /**
     * 区域码
     */
    public String reqReserved1 = "";
    public String reqReserved2 = "";

    private XmlTextReader xmlReader;
    private String detailString;

    public int detailCount = -1;
    
    /**协议编号*/
    public String authAgNum;
    /**缴费种类*/
    private String authAgName;
    
    /**获取缴费种类*/
    public String getAuthAgName(){
    	if(StringHelper.hasAnyChar(payType)){
    		return payType;
    	}else if(StringHelper.hasAnyChar(authAgName)){
    		return authAgName;
    	}
    	return "";
    }
    
    /**是否为单笔签约的请求，复用批量签约接口用*/
    private boolean isFromReqContract = false;
    private int readIndex=0;
    private ReqAuthForRecDetail preReadDetail;
    public ReqAuthForRec(ReqContract p_req){
    	readIndex=0;
    	preReadDetail=null;
    	isFromReqContract = true;
    	reqHeader = p_req.reqHeader;
    	this.authAgNum = p_req.contractDetail.authAgNum;
    	this.ownAct = p_req.contractDetail.recAct;
    	this.totalNum = "1";
    	this.reqSeqID = p_req.contractDetail.askNumber;
    	preReadDetail = new ReqAuthForRecDetail(p_req.contractDetail);
    }
    

    public ReqAuthForRec(XmlTextReader p_xmlReader, ReqRespHeader p_header) throws Exception {
        reqHeader = p_header;
        xmlReader = p_xmlReader;
        totalNum = xmlReader.getSimpleText("TotalNum");
        reqSeqID = xmlReader.getSimpleText("ReqSeqID");
        payType = xmlReader.getSimpleText("PayType");
        authAgNum = xmlReader.getSimpleTextNotNull("AuthAgNum");
        authAgName = xmlReader.getSimpleTextNotNull("AuthAgName");
        ownActName = xmlReader.getSimpleText("AcctName");
        ownAct = xmlReader.getSimpleText("AcctNo");
        reqReserved1 = xmlReader.getSimpleText("ReqReserved1");
        reqReserved2 = xmlReader.getSimpleText("ReqReserved2");
        detailString = xmlReader.getSimpleText("DetailRecord");
        beginIndex = 0;
        endIndex = 0;
        
        /** ATS报文明细 DetailRecord中每笔明细末尾加\n分割,最后一笔不加\n*/
        if (StringHelper.hasAnyChar(detailString)) {
        	if (detailString.contains("\n")) {
            	detailCount = detailString.split("\n").length;
            } else {
            	detailCount = 1;
            }
        } else {
        	detailCount = Integer.valueOf(totalNum);
        }
        
    }

    public ReqAuthForRecDetail readedDetail;
    private int beginIndex;
    private int endIndex;

    public boolean readRecord() throws Exception {
        readedDetail = null;
        if(isFromReqContract){//为单笔签约的请求
        	if(readIndex==0){
        		readIndex++;
        		readedDetail = preReadDetail;
        		return true;
        	}else{
        		return false;
        	}
        }
        if (!detailString.contains("||")) {
            if (xmlReader.read(PATH_Detail)) {
                readedDetail = new ReqAuthForRecDetail(xmlReader.readedXElement);
                return true;
            } else {
                return false;
            }
        } else {
            if (endIndex < 0) {
                return false;
            }
            int lenStr = detailString.length();
            while (endIndex < lenStr) {
                if (detailString.charAt(endIndex) == '\n' || endIndex == lenStr - 1) {
                    String subStr = detailString.substring(beginIndex, endIndex + 1);
                    readedDetail = new ReqAuthForRecDetail(subStr);
                    beginIndex = endIndex + 1;
                    endIndex = beginIndex;
                    return true;
                }
                if (endIndex < lenStr - 1) {
                    endIndex++;
                }
            }
            beginIndex = -1;
            endIndex = -1;
            return false;
        }
    }
}
